package managers.dateManager.endFrozen.endChange;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.constants.TimeConstants;
import org.tbjd.client.exceptions.TaskManagerException;
import org.tbjd.client.managers.TaskManager;
import org.tbjd.client.states.StateBeginFrozenSPEC;

import factories.TaskFactory;

/**
 * <p>
 * Test TaskManager's methods
 * </p>
 * <p>
 * <b>Case</b>
 * </p>
 * <p>
 * A task with multiple predecessors and successors as well as isolated tasks
 * </p>
 * <p>
 * <b>Action</b>
 * </p>
 * <p>
 * EndDate of the task "task" is changed
 * </p>
 * <b> Expected</b>
 * <ul>
 * <li>update Duration of "task"</li>
 * </ul>
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public class EndFrozen_EndChange_UpdatePredecessorsAndSuccessors_Basic extends TestCase {

	private Map<Integer, Object> map = null;
	private Logger log = Logger.getLogger("console");
	private DateFormat df = DateFormat.getInstance();
	private Task task = null;
	private static final long ADDED_DAYS = 2;
	private TaskManager dateManager = new TaskManager();

	protected void setUp() throws Exception {
		df = new SimpleDateFormat("dd.MM.yyyy");

		// create a list of tasks with pred. / succ.
		// here, task has 2 predecessors (p1 & p2) and 2 successors (s1 & s2)
		// o p1 manual
		// o t1 manual
		// o task manual
		// o t2 manual
		// o s1 manual

		Task p1 = TaskFactory.getInstance();
		p1.setName("p1");
		p1.setBeginDate(df.parse("1.1.2009"));
		p1.setEndDate(df.parse("2.1.2009"));
		p1.setDuration(24);
		p1.setState(new StateBeginFrozenSPEC(p1.getState().getDurationModeState()));

		Task t1 = TaskFactory.getInstance();
		t1.setName("t1");
		t1.setBeginDate(df.parse("1.5.2009"));
		t1.setEndDate(df.parse("3.5.2009"));
		t1.setDuration(48);
		t1.setState(new StateBeginFrozenSPEC(t1.getState().getDurationModeState()));

		this.task = TaskFactory.getInstance();
		this.task.setName("task");
		this.task.setBeginDate(df.parse("4.1.2009"));
		this.task.setEndDate(df.parse("10.1.2009"));
		this.task.setDuration(144);
		this.task.setState(new StateBeginFrozenSPEC(this.task.getState().getDurationModeState()));

		Task t2 = TaskFactory.getInstance();
		t2.setName("t2");
		t2.setBeginDate(df.parse("1.1.2009"));
		t2.setEndDate(df.parse("3.1.2009"));
		t2.setDuration(48);
		t2.setState(new StateBeginFrozenSPEC(t2.getState().getDurationModeState()));

		Task s1 = TaskFactory.getInstance();
		s1.setName("s1");
		s1.setBeginDate(df.parse("11.1.2009"));
		s1.setEndDate(df.parse("14.1.2009"));
		s1.setDuration(72);
		s1.setState(new StateBeginFrozenSPEC(s1.getState().getDurationModeState()));

		p1.addSuccessor(this.task);
		this.task.addSuccessor(s1);

		super.setUp();
	}

	public void testListNotNull() {
		System.out.println("");
		log.debug(" === testListNotNull");

		Date newEndDate = new Date(this.task.getEndDate().getTime() + (ADDED_DAYS * TimeConstants.DAY_IN_MILLIS));
		log.debug("new EndDate = " + df.format(newEndDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_endDate, newEndDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		try {
			assertNotNull("Updated bean list isn't null", dateManager.manageDates(map));

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

	}

	public void testListSize() {
		System.out.println("");
		log.debug(" === testListSize");

		Date newEndDate = new Date(this.task.getEndDate().getTime() + (ADDED_DAYS * TimeConstants.DAY_IN_MILLIS));
		log.debug("new EndDate = " + df.format(newEndDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_endDate, newEndDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}
		assertEquals("Updated bean list size = 1", 1, updatedBeans.size());

	}

	public void testBeanInstance() {
		System.out.println("");
		log.debug(" === testReturnedBeanInstance");

		Date newEndDate = new Date(this.task.getEndDate().getTime() + (ADDED_DAYS * TimeConstants.DAY_IN_MILLIS));
		log.debug("new EndDate = " + df.format(newEndDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_endDate, newEndDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		assertEquals(this.task, updatedBeans.get(0));

	}

	public void testDates() {
		System.out.println("");
		log.debug(" === testDates");

		Date originalBeginDate = this.task.getBeginDate();
		Date originalEndDate = this.task.getEndDate();

		Date newEndDate = new Date(this.task.getEndDate().getTime() + (ADDED_DAYS * TimeConstants.DAY_IN_MILLIS));
		log.debug("new EndDate = " + df.format(newEndDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_endDate, newEndDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("original BeginDate = " + originalBeginDate);
		log.debug("original EndDate = " + originalEndDate);
		log.debug("new EndDate = " + newEndDate);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		for (Task updatedBean : updatedBeans) {
			log.debug(updatedBean.getName() + " \t updated with BeginDate = " + df.format(updatedBean.getBeginDate()) + " , EndDate = "
					+ df.format(updatedBean.getEndDate()));
			try {
				if (updatedBean.getName().equalsIgnoreCase("p1")) {
					fail();
				}
				if (updatedBean.getName().equalsIgnoreCase("t1")) {
					fail();
				}
				if (updatedBean.getName().equalsIgnoreCase("task")) {
					assertEquals("check task BeginDate", df.parseObject("4.1.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("12.1.2009"), updatedBean.getEndDate());
				}
				if (updatedBean.getName().equalsIgnoreCase("t2")) {
					fail();
				}
				if (updatedBean.getName().equalsIgnoreCase("s2")) {
					fail();
				}

			} catch (ParseException e) {
				fail(e.getMessage());

			}
		}
	}

	public void testDurations() {
		System.out.println("");
		log.debug(" === testDurations");

		float originalDuration = this.task.getDuration();

		Date newEndDate = new Date(this.task.getEndDate().getTime() + (ADDED_DAYS * TimeConstants.DAY_IN_MILLIS));
		log.debug("new EndDate = " + df.format(newEndDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_endDate, newEndDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("original duration = " + originalDuration);
		log.debug("new BeginDate = " + newEndDate);

		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		for (Task updatedBean : updatedBeans) {
			if (updatedBean.getName().equalsIgnoreCase("p1")){
				fail();
			}
			if (updatedBean.getName().equalsIgnoreCase("t1")){
				fail();
			}
			if (updatedBean.getName().equalsIgnoreCase("task")){
				assertEquals(originalDuration+(ADDED_DAYS*24), updatedBeans.get(0).getDuration());
			}
			if (updatedBean.getName().equalsIgnoreCase("t2")){
				fail();
			}
			if (updatedBean.getName().equalsIgnoreCase("s1")){
				fail();
			}
			
		}
	}

	public void testRevertDates() {
		System.out.println("");
		log.debug(" === testRevertDates");

		Date originalBeginDate = this.task.getBeginDate();
		Date originalEndDate = this.task.getEndDate();

		Date newEndDate = new Date(this.task.getEndDate().getTime() + (ADDED_DAYS * TimeConstants.DAY_IN_MILLIS));
		log.debug("new EndDate = " + df.format(newEndDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_endDate, newEndDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("original beginDate = " + originalBeginDate);
		log.debug("original endDate = " + originalEndDate);
		log.debug("new newEndDate = " + newEndDate);

		try {
			dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		log.debug("call dateManager with original BeginDate...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_endDate, originalEndDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		for (Task updatedBean : updatedBeans) {
			log.debug(updatedBean.getName() + " \t updated with BeginDate = " + df.format(updatedBean.getBeginDate()) + " , EndDate = "
					+ df.format(updatedBean.getEndDate()));
			try {
				if (updatedBean.getName().equalsIgnoreCase("p1")) {
					fail();
				}
				if (updatedBean.getName().equalsIgnoreCase("t1")) {
					fail();
				}
				if (updatedBean.getName().equalsIgnoreCase("task")) {
					assertEquals("check task BeginDate", df.parseObject("4.1.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("10.1.2009"), updatedBean.getEndDate());
				}
				if (updatedBean.getName().equalsIgnoreCase("t2")) {
					fail();
				}
				if (updatedBean.getName().equalsIgnoreCase("s2")) {
					fail();
				}

			} catch (ParseException e) {
				fail(e.getMessage());

			}
		}
	}

	public void testRevertDurations() {
		System.out.println("");
		log.debug(" === testRevertDurations");

		Date originalBeginDate = this.task.getBeginDate();
		Date originalEndDate = this.task.getEndDate();
		float originalDuration = this.task.getDuration();
		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("31.5.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}

		log.debug("original beginDate = " + originalBeginDate);
		log.debug("original endDate = " + originalEndDate);
		log.debug("original duration = " + originalDuration);
		log.debug("new BeginDate = " + newBeginDate);

		log.debug("call dateManager with new BeginDate = " + newBeginDate + "...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		try {
			dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		log.debug("call dateManager with original BeginDate = " + originalBeginDate + "...");
		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, originalBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());
		}

		for (Task updatedBean : updatedBeans) {
			if (updatedBean.getName().equalsIgnoreCase("p1")){
				fail();
			}
			if (updatedBean.getName().equalsIgnoreCase("t1")){
				fail();
			}
			if (updatedBean.getName().equalsIgnoreCase("task")){
				assertEquals(originalDuration, updatedBeans.get(0).getDuration());
			}
			if (updatedBean.getName().equalsIgnoreCase("t2")){
				fail();
			}
			if (updatedBean.getName().equalsIgnoreCase("s1")){
				fail();
			}
			
		}
	}

}